package com.example.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.domain.TuShu;
import com.example.domain.XueSheng;
import com.example.service.XueShengService;
import com.example.util.HttpUtil;

public class XueShengServiceImpl implements XueShengService {

	@Override
	public XueSheng login(String xs_xh, String xs_mm) {
		String servlet = "XueShengServlet";
		String method = "login";
		Map<String, String> map = new HashMap<String, String>();
		map.put("xs_xh", xs_xh);
		map.put("xs_mm", xs_mm);
	    String result =	HttpUtil.sendHttp(servlet, method, map);
	    String result1 = result.trim();
	    boolean flag="false".equals(result1);
		if (!flag) {
			try {
				JSONObject jsonObject = new JSONObject(result);
				JSONObject object = jsonObject.getJSONObject("xuesheng");
				XueSheng xueSheng = new XueSheng();
				xueSheng.setXs_bj(object.getString("xs_bj"));
				xueSheng.setXs_dh(object.getString("xs_dh"));
				xueSheng.setXs_mm(object.getString("xs_mm"));
				xueSheng.setXs_xh(Integer.valueOf(object.getString("xs_xh")));
				xueSheng.setXs_xm(object.getString("xs_xm"));
				xueSheng.setXs_xy(object.getString("xs_xy"));
				xueSheng.setXs_zy(object.getString("xs_zy"));
				return xueSheng;
			} catch (JSONException e) {
				e.printStackTrace();
			}
			}
				return null;
		
	}

	@Override
	public boolean save(XueSheng xueSheng) {
		String servlet = "XueShengServlet";
		String method = "save";
		Map<String, String> map = new HashMap<String, String>();
		map.put("xs_bj", xueSheng.getXs_bj());
		map.put("xs_dh", xueSheng.getXs_dh());
		map.put("xs_mm", xueSheng.getXs_mm());
		map.put("xs_xm", xueSheng.getXs_xm());
		map.put("xs_xy", xueSheng.getXs_xy());
		map.put("xs_zy", xueSheng.getXs_zy());
		map.put("xs_xh", String.valueOf(xueSheng.getXs_xh()));
		String result = HttpUtil.sendHttp(servlet, method, map);
		if ("true".equals(result)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean update(XueSheng xueSheng) {
		String servlet = "XueShengServlet";
		String method = "update";
		Map<String, String> map = new HashMap<String, String>();
		map.put("xs_bj", xueSheng.getXs_bj());
		map.put("xs_dh", xueSheng.getXs_dh());
		map.put("xs_mm", xueSheng.getXs_mm());
		map.put("xs_xm", xueSheng.getXs_xm());
		map.put("xs_xy", xueSheng.getXs_xy());
		map.put("xs_zy", xueSheng.getXs_zy());
		map.put("xs_xh", String.valueOf(xueSheng.getXs_xh()));
		String result = HttpUtil.sendHttp(servlet, method, map);
		if ("true".equals(result)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<TuShu> getXueShengJieYueXinXis(String xs_xh) {
		String servlet = "XueShengServlet";
		String method = "getXueShengJieYueXinXis";
		Map<String, String> map = new HashMap<String, String>();
		map.put("xs_xh", xs_xh);
		String resutl = HttpUtil.sendHttp(servlet, method, map);
		List<TuShu> list = new ArrayList<TuShu>();
		if (!"false".equals(resutl)) {
			try {

				JSONObject jsonObject = new JSONObject(resutl);
				JSONArray jsonArray = jsonObject
						.getJSONArray("getXueShengJieYueXinXis");

				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject object = jsonArray.getJSONObject(i);
					TuShu tuShu = new TuShu();
					tuShu.setTs_id(object.getInt("ts_id"));
					tuShu.setJy_sj(object.getString("jy_sj"));
					tuShu.setTs_cbs(object.getString("ts_cbs"));
					tuShu.setTs_jj(object.getString("ts_jj"));
					tuShu.setTs_sm(object.getString("ts_sm"));
					tuShu.setTs_zz(object.getString("ts_zz"));
					list.add(tuShu);
				}
				return list;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
